﻿using ProjectScheduling.CalendarControl;
using ProjectScheduling.daos;
using SqlSugar;
using static System.Net.Mime.MediaTypeNames;

namespace ProjectScheduling
{
    public partial class HomePage : UserControl
    {
        private DateTime _day = DateTime.Today;
        public static readonly HomePage b = new();
        public DateTime CurrDay
        {
            get
            {
                return _day;
            }
            set
            {
                _day = value;
                txt_day.Text = value.ToString("yyyy-MM");
            }
        }

        bool isSearch = false;
        public HomePage()
        {
            InitializeComponent();
            MonthPager.b.CurrDay = CurrDay = DateTime.Today;
            MonthPager.b.Dock = DockStyle.Fill;
            WeekPager.b.Dock = DockStyle.Fill;
            //combo_users.DrawMode = DrawMode.OwnerDrawFixed;
            //combo_users.DrawMode = DrawMode.OwnerDrawVariable;
            var allusers = StaffDao.Base.Search(it => true, it => it.TrueName);
            MonthPager.b.allUserAction = (list) =>
            {
                list = list.Select(it => it.Trim()).ToList();
                try
                {
                    isSearch = true;

                    this.Invoke(() =>
                    {
                        var txt = combo_users.Text;
                        combo_users.Items.Clear();
                        combo_users.Items.Add("全部");
                        combo_users.Items.AddRange([.. list]);
                        allusers.Where(it => !list.Contains(it ?? "")).ToList().ForEach(it => combo_users.Items.Add(" " + it));
                        combo_users.Text = string.IsNullOrEmpty(txt) ? "全部" : txt;
                    });
                }
                catch (Exception)
                {
                    try
                    {
                        var txt = combo_users.Text;
                        combo_users.Items.Clear();
                        combo_users.Items.Add("全部");
                        combo_users.Items.AddRange([.. list]);
                        allusers.Where(it => !list.Contains(it ?? "")).ToList().ForEach(it => combo_users.Items.Add(" " + it));
                        combo_users.Text = string.IsNullOrEmpty(txt) ? "全部" : txt;
                    }
                    catch (Exception)
                    {

                    }
                }

            };
            MonthPager.b.clickAction = (time) =>
            {
                CurrDay = time;
            };
            p_main.Controls.Clear();
            p_main.Controls.Add(MonthPager.b);
        }

        private void Panel1_Resize(object sender, EventArgs e)
        {
            p_time.Location = new Point(Width / 2 - 100, 10);
        }

        private void Btn_pre_Click(object sender, EventArgs e)
        {
            MonthPager.b.CurrDay = CurrDay = DateTime.Parse(CurrDay.ToString("yyyy-MM-01")).AddMonths(-1);

        }

        private void Btn_next_Click(object sender, EventArgs e)
        {
            MonthPager.b.CurrDay = CurrDay = DateTime.Parse(CurrDay.ToString("yyyy-MM-01")).AddMonths(1);
        }

        private void combo_users_SelectedIndexChanged(object sender, EventArgs e)
        {
            MonthPager.b.CurrentUserName = combo_users.Text == "全部" ? "" : combo_users.Text;
            if (!isSearch)
            {
                MonthPager.b.ChangeFunc(CurrDay);
            }
            isSearch = false;
            //MonthPager.b.CurrDay = CurrDay;
        }

        private void combo_users_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) return;
            var item = combo_users.Items[e.Index]?.ToString() ?? "";
            Color textColor;
            if (item[0] == ' ')
            {
                textColor = Color.Gray;
            }
            else
            {
                textColor = Color.Black;
            }
            e.Graphics.DrawString(item.Trim(), e.Font ?? new System.Drawing.Font("宋体", 12), new SolidBrush(textColor), new PointF(e.Bounds.X, e.Bounds.Y));
        }
    }
}
